﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.EquipmentManageDomain
{
    public class DispatchEquipmentService : IDispatchEquipmentService
    {
        private readonly ILogUtil _logUtil;
        private readonly IDispatchEquipmentRepository _dispatchEquipmentRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;


        public DispatchEquipmentService(ISnowflake snowflake, IDispatchEquipmentRepository dispatchEquipmentRepository, ILogUtil logUtil, IMapper mapper)
        {
            _dispatchEquipmentRepository = dispatchEquipmentRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _Snowflake = snowflake;
        }

        public async Task<long> AddDispatchEquipmentAsync(DispatchEquipmentInfoDto dispatchEquipmentInfoDto)
        {
            //DispatchEquipmentInfoDto为起始，DispatchEquipmentInfo为目标
            var dispatchEquipmentInfo = _Mapper.Map<DispatchEquipmentInfoDto, DispatchEquipmentInfo>(dispatchEquipmentInfoDto);
            long id = _Snowflake.GetId();
            dispatchEquipmentInfo.ID = id;
            bool result = await _dispatchEquipmentRepository.InsertAsync(dispatchEquipmentInfo);
            return result ? id : 0;
        }

        public async Task<bool> DeleteDispatchEquipmentAsync(long ID)
        {
            bool result = await _dispatchEquipmentRepository.DeleteAsync(it => it.ID == ID);
            return result;
        }

        public async Task<bool> UpdateDispatchEquipmentAsync(DispatchEquipmentInfoDto dispatchEquipmentInfoDto)
        {
            var dispatchEquipmentInfo = _Mapper.Map<DispatchEquipmentInfoDto, DispatchEquipmentInfo>(dispatchEquipmentInfoDto);
            bool result = await _dispatchEquipmentRepository.UpdateAsync(dispatchEquipmentInfo);
            return result;
        }

        public async Task<PageDto<DispatchEquipmentInfoDto>> GetDispatchEquipmentAsync(QueryDispatchEquipmentInfoDto queryDispatchEquipmentInfoDto)
        {
            PageModel page = new PageModel { PageIndex = queryDispatchEquipmentInfoDto.PageIndex, PageSize = queryDispatchEquipmentInfoDto.PageSize, };
            var equipmentName = queryDispatchEquipmentInfoDto.EquipmentName;
            var equipmentCode = queryDispatchEquipmentInfoDto.EquipmentCode;
            var equipmentTypeID = queryDispatchEquipmentInfoDto.EquipmentTypeID;
            var exp = Expressionable.Create<DispatchEquipmentInfo, EquipmentTypeInfo>();
            exp.AndIF(!string.IsNullOrEmpty(equipmentName), (a, b) => a.EquipmentName.Contains(equipmentName));
            exp.AndIF(!string.IsNullOrEmpty(equipmentCode), (a, b) => a.EquipmentCode == equipmentCode);
            exp.AndIF(equipmentTypeID > 0, (a, b) => a.EquipmentTypeID == equipmentTypeID);

            return await _dispatchEquipmentRepository.GetDispatchEquipmentAsync(exp.ToExpression(), page);
        }

        public async Task<DispatchEquipmentInfoDto> GetDispatchEquipmentByIDAsync(long ID)
        {
            var result = await _dispatchEquipmentRepository.GetByIdAsync(ID);
            var vModel = _Mapper.Map<DispatchEquipmentInfo, DispatchEquipmentInfoDto>(result);
            return vModel;
        }

        public async Task<List<DispatchEquipmentInfoDto>> GetAllDispatchEquipmentAsync()
        {
            List<DispatchEquipmentInfoDto> dispatchEquipmentInfoDtos = new List<DispatchEquipmentInfoDto>();
            List<DispatchEquipmentInfo> dispatchEquipmentInfos = await _dispatchEquipmentRepository.GetListAsync();
            _Mapper.Map(dispatchEquipmentInfos, dispatchEquipmentInfoDtos);
            return dispatchEquipmentInfoDtos;
        }
    }
}
